package com.cjzuji.rental.controller.order;

import com.cjzuji.rental.service.ExcelImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;

@RestController
@RequestMapping("/api/import")
//http://127.0.0.1:9099/api/import/
public class ImportController {
    @Autowired
    ExcelImportService excelImportService;
    /**
     * 导入佣金
     * @param filePath
     * @return
     */
    //@Transactional
    @PutMapping("/commission")
    public Object importCommission(@RequestParam("filePath")String  filePath){
        try {
            //System.out.println("文件路径~~"+ filePath);
            File file = new File(filePath);
            if (!file.exists()){
                return ResponseEntity.badRequest().body("文件不存在！");
            }
            String result = excelImportService.CommissionExcel(file);
            return ResponseEntity.ok(result);
        }catch (Exception e){
            return ResponseEntity.internalServerError().body("导入失败："+e.getMessage());
        }
    }

    /**
     * 导入消费记录
     * @param filePath
     * @return
     */
   // @Transactional
    @PutMapping("/consumptionRecord")
    public Object importConsumptionRecord(@RequestParam("filePath")String  filePath){
        try {
            System.out.println("文件路径~~"+ filePath);
            File file = new File(filePath);
            if (!file.exists()){
                return ResponseEntity.badRequest().body("文件不存在！");
            }
            String result = excelImportService.ConsumptionRecordExcel(file);
            return ResponseEntity.ok(result);
        }catch (Exception e){
            return ResponseEntity.internalServerError().body("导入失败："+e.getMessage());
        }
    }

    /**
     * 导入采购单
     * @param filePath
     * @return
     */
    @PutMapping("/purchaseBill")
    public Object importPurchaseBill(@RequestParam("filePath")String  filePath){
        try {
            System.out.println("文件路径~~"+ filePath);
            File file = new File(filePath);
            if (!file.exists()){
                return ResponseEntity.badRequest().body("文件不存在！");
            }
            String result = excelImportService.PurchaseBillExcel(file);
            return ResponseEntity.ok(result);
        }catch (Exception e){
            return ResponseEntity.internalServerError().body("导入失败："+e.getMessage());
        }
    }
}
